<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            height: 0;
        }
        .box1 {
            width:500px;
            height: 100px;
            margin: 50px auto;
            border: solid 1px black;
            position: relative;
        }
        .box1 p {
            position: absolute;
            left:50%;
            /* 方法一：left 50%;然后利用向左位移50% */
            transform: translate(-50%);
            width: 100px;
            height: 100px;
            background-color: red;
        }

        .box2 {
            width:500px;
            height: 100px;
            margin: 50px auto;
            border: solid 1px black;
            position: relative;
        }
        .box2 p {
            position: absolute;
            left:50%;
            /* 方法二、left 50%;然后利用margin-left往左边移动宽度的一半 */
            margin-left:-50px;
            width: 100px;
            height: 100px;
            background-color: blue;
        }

        .box3 {
            width:500px;
            height: 100px;
            margin: 50px auto;
            border: solid 1px black;
        }
        .box3 p {
            /* 方法三、margin: auto,左右一样的margin */
            margin: auto;
            width: 100px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="box1">
        <p></p>
    </div>
    <div class="box2">
        <p></p>
    </div>
    <div class="box3">
        <p></p>
    </div>
</body>
</html>